package com.doone.telmarket.action.admin;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.domain.GuangGao;
import com.doone.telmarket.service.GuangGaoService;
import com.doone.telmarket.service.SiteResourceManager;
import com.opensymphony.xwork2.ActionContext;

public class GuangGaoAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private File uploadFile;

	private String url;

	private String id;

	private String imgPath;

	private String errorMessage;

	private String page;

	private List<GuangGao> guangGaoList;

	@Autowired
	private GuangGaoService remoteGuangGaoService;

	@Autowired
	private SiteResourceManager siteResourceManager;

	@Action(value = "updateGuangGao", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "success", location = "/admin/success.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String updateGuangGao() {
		try {

			if (uploadFile != null) {
				GuangGao g = remoteGuangGaoService.getByKey(Integer.parseInt(id));

				System.out.println(id);
				System.out.println(g);

				String imgpath = g.getImgPath();
				siteResourceManager.uploadFile(uploadFile, imgpath);
			}
			GuangGao g1 = new GuangGao();
			g1.setId(Integer.parseInt(id));
			g1.setUrl(url);
			remoteGuangGaoService.updateGuangGao(g1);
			page = "queryAllGuangGao2";
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMessage = "更新失败";
			return "error";
		}
	}

	@SuppressWarnings("static-access")
	@Action("queryAllGuangGao")
	public void queryAllGuangGao() {
		try {
			List<GuangGao> li = remoteGuangGaoService.selectAllGuangGao();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer sb = new StringBuffer();
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://sc.ct10000.com");
			//pagePath.append(sr.getRemoteAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());

			for (int i = 0; i < li.size(); i++) {
				sb
						.append("<table width=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-top:8px;\">"
								+ "<tr><td><a  href=\""
								+ li.get(i).getUrl()
								+ "\"   target=\"_blank\">"
								+ "<img src=\""
								+ pagePath
								+ "/"
								+ li.get(i).getImgPath()
								+ "\""
								+ " width=\"180\" height=\"115\" /></a></td>" + "</tr></table>");
			}
			JSONUtil js = new JSONUtil();
			responseWrite(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action(value = "queryAllGuangGao2", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "success", location = "/admin/toModifyGuangGao.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String queryAllGuangGao2() {
		try {
			guangGaoList = remoteGuangGaoService.selectAllGuangGao();
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMessage = "查询广告失败";
			page = "uploadIndexImage.jsp";
			return "error";
		}
	}

	@Action(value = "deleteGuangGao", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "success", location = "/admin/success.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String deleteGuangGao() {
		try {
			remoteGuangGaoService.deleteGuangGao(Integer.parseInt(id));
			page = "queryAllGuangGao2";
			return "success";
		} catch (Exception e) {
			e.printStackTrace();

			return "error";
		}
	}

	@Action(value = "insertGuangGao", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "success", location = "/admin/success.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String insertGuangGao() {
		try {
			String fileName = "//images//service//" + new SimpleDateFormat("yyyyMMssHHmmss").format(new Date())
					+ ".jpg";
			siteResourceManager.uploadFile(uploadFile, fileName);
			GuangGao g = new GuangGao();
			g.setImgPath(fileName);
			g.setUrl(url);
			page = "uploadIndexImage.jsp";
			remoteGuangGaoService.insertGuangGao(g);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			errorMessage = "保存失败";
			page = "uploadIndexImage.jsp";
			return "error";
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public List<GuangGao> getGuangGaoList() {
		return guangGaoList;
	}

	public void setGuangGaoList(List<GuangGao> guangGaoList) {
		this.guangGaoList = guangGaoList;
	}

}
